package app.species;

import sim.engine.*;
import sim.util.*;

@SuppressWarnings("unused")
public class SpinningTurret extends Entity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7620903223039878134L;

	private static int SHOOT_INTERVAL = 50;

	private static double DEFAULT_DIAMETER = 75.0;
	private static double DEFAULT_SENSE_DIST = 75.0;
	private static double DEFAULT_SPEED = 10.0;
	private static int DEFAULT_LIFE_SPAN = 0;

	private int timeSinceShot;

	public SpinningTurret(MutableDouble2D initPos, MutableDouble2D initHead){

		super(initPos, initHead);

		this.setDiameter(SpinningTurret.DEFAULT_DIAMETER);
		this.setSenseDistance(SpinningTurret.DEFAULT_SENSE_DIST);
		this.setSpeed(SpinningTurret.DEFAULT_SPEED);

		this.resetColor();

		// WILL CHANGE!!!!!!
		this.setEnergy(1000);

		this.timeSinceShot = 0;

	}

	public SpinningTurret(){ super(); }

	public static void setDefaults(double diameter, double speed, double senseDist, int lifeSpan){

		SpinningTurret.DEFAULT_DIAMETER = diameter;
		SpinningTurret.DEFAULT_SPEED = speed;
		SpinningTurret.DEFAULT_SENSE_DIST = senseDist;
		SpinningTurret.DEFAULT_LIFE_SPAN = lifeSpan;


	}

	public static void setShootInterval(int n){

		SpinningTurret.SHOOT_INTERVAL = n;

	}

	/* simply updates the agent's position based off of its heading and current position
	 **/
	public void step(SimState state){

		SpeciesSim sim = (SpeciesSim)state;

		if(this.timeSinceShot >= SpinningTurret.SHOOT_INTERVAL){
			this.shootBullet(sim);
			this.timeSinceShot = 0;
		}else{
			this.timeSinceShot++;
		}

		this.heading.rotate(.025);

	}

	private void shootBullet(SpeciesSim sim){

		Bullet newBullet;

		newBullet = new Bullet(new MutableDouble2D(this.position), new MutableDouble2D(this.heading));

		sim.bullets.setObjectLocation(newBullet, new Double2D(this.position));
		newBullet.stoppable = sim.schedule.scheduleRepeating(newBullet);

	}

}
